package me.miencraft.plugin.superlobby.item;

import lombok.Getter;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Objects;

/**
 * @author langle__
 * @version 1.0
 */
@Getter
public class ItemProperties {

    @NotNull private final Material material;
    @Nullable private final String name;
    @Nullable private final List<String> lore;

    public ItemProperties(@NotNull Material material, @Nullable String name, @Nullable List<String> lore) {
        this.material = material;
        this.name = name;
        this.lore = lore;
    }

    public ItemProperties(Material material, String name) {
        this(material, name, null);
    }

    public ItemProperties(Material material) {
        this(material, null, null);
    }

    public ItemProperties(ItemStack item) {
        this.material = item.getType();
        ItemMeta meta = item.getItemMeta();
        this.name = meta != null ? meta.getDisplayName() : null;
        this.lore = meta != null ? meta.getLore() : null;
    }

    public boolean validate(ItemStack item) {
        ItemProperties properties = new ItemProperties(item);
        return validate(properties);
    }

    public boolean validate(CustomItem item) {
        ItemProperties properties = item.getProperties();
        return validate(properties);
    }

    public boolean validate(ItemProperties properties) {
        return this.material == properties.material && Objects.equals(this.name, properties.name) && Objects.equals(this.lore, properties.lore);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ItemProperties that = (ItemProperties) o;
        return material == that.material && Objects.equals(name, that.name) && Objects.equals(lore, that.lore);
    }

    @Override
    public int hashCode() {
        return Objects.hash(material, name, lore);
    }

    public ItemStack toItemStack() {
        ItemStack stack = new ItemStack(material);
        ItemMeta meta = stack.getItemMeta();
        if (meta != null) {
            meta.setDisplayName(name);
            meta.setLore(lore);
            stack.setItemMeta(meta);
        }
        return stack;
    }

}
